package com.waimai.app.qa.data;

import java.util.Hashtable;

import org.jsefa.csv.annotation.CsvDataType;
import org.jsefa.csv.annotation.CsvField;

import com.waimai.app.qa.webdriver.AppElement;

/**
 * @author AiHui
 * 
 */
@CsvDataType()
public class Item extends AppElement implements Cloneable{

	@CsvField(pos = 1)
	public String elementName;
	
	@CsvField(pos = 2)
	public String testData;
	
	@CsvField(pos = 3)
	public String locatorType;
	
	@CsvField(pos = 4)
	public String locatorValue;
	
	/**
	 * testdata format: dataName1<n>dataValue1<d>dataName2<n>dataValue2
	 * Useage: 
	 *   Hashtable userNameDatas = login.UserName.getDatas();
	 *   System.out.println("dataName1: " + userNameDatas.get("dataName1"));
	 */	
	public Hashtable<String, String> getDatas() {
		Hashtable<String, String> datas = new Hashtable<String, String>();
		String[] ss = this.testData.split("<d>");
		for (int i = 0; i < ss.length; i++) {
			String s = ss[i].trim();
			if ((s != null) && !s.equals("")) {
				String[] s1 = s.split("<n>");
				datas.put(s1[0].trim(), s1[1].trim());
			}
		}
		return datas;
	}
	
	/**
	 * get the value of testData hash table by key
	 * 
	 * @param key
	 * @return
	 */
	public String get(String key) {
		return getDatas().get(key);
	}
	
	/**
	 * Implementing clone interface
	 */
	public Object clone(){
		Item ite = null;
		try {   
			ite = (Item)super.clone();   
        } catch (CloneNotSupportedException e) {   
        	e.printStackTrace();   
        }   
		return ite;
	}
}
